
#include <SDL/SDL.h>
#include "input.h"

/* Poll for a certain amount of events. */

Inputevent key2event(SDL_Event event, int change) {
	Inputevent result;
	result.change = change;
	switch (event.key.keysym.sym ) {
		case SDLK_UP: 
			result.player = 1; // (first player)
			result.button = INPUT_UP;
		break;
		case SDLK_DOWN:
			result.player = 1; // (first player)
			result.button = INPUT_DOWN;
		break;
		case SDLK_RIGHT:
			result.player = 1; // (first player)
			result.button = INPUT_RIGHT;
		break;
		case SDLK_LEFT:
			result.player = 1; // (first player)
			result.button = INPUT_RIGHT;
		break;
		case SDLK_BREAK:
			result.player = 1; // (first player)
			result.button = INPUT_QUIT;			
		break;
		case SDLK_ESCAPE:
			result.player = 1; // (first player)
			result.button = INPUT_ESCAPE;			
		break;
		case SDLK_RSHIFT:
			result.player = 1; // (first player)
			result.button = INPUT_B1;			
		break;
		case SDLK_RCTRL:
			result.player = 1; // (first player)
			result.button = INPUT_B2;			
		break;
		case SDLK_RETURN:
			result.player = 1; // (first player)
			result.button = INPUT_B3;			
		break;
		case SDLK_BACKSPACE:
			result.player = 1; // (first player)
			result.button = INPUT_B4;			
		break;
		default:
			result.player = 1; // (first player)
			result.button = INPUT_OTHER;			
		break;
	}
	return result;
}

Inputevent joybutton2event(SDL_Event event, int change) {
	Inputevent result;
	result.change = change;
	result.player = event.jbutton.which + 1;
	switch(event.jbutton.button) {
		case 0: result.button  = INPUT_B1; break;
		case 1: result.button  = INPUT_B2; break;
		case 2: result.button  = INPUT_B3; break;
		case 3: result.button  = INPUT_B4; break;
		case 4: result.button  = INPUT_B5; break;
		case 5: result.button  = INPUT_B6; break;
		case 6: result.button  = INPUT_B7; break;
		case 7: result.button  = INPUT_B8; break;
		default: result.button = INPUT_OTHER; break;
	}
	return result;
} 

#define JOYSTICK_X_AXIS 0
#define JOYSTICK_Y_AXIS 1
#define JOYSTICK_DELTA  100

/*
int joystick_direction(int axis, int value) {
	if(axis == JOYSTICK_X_AXIS) {
		if(value > JOYSTICK_DELTA )	{
			return INPUT_RIGHT;
		} else if (value  < - JOYSTICK_DELTA )	{
		} else {
			return INPUT_
		}
	}
}
*/
/*
Inputevent joyaxis2event(SDL_Event event) {
	Inputevent result;
    result.player   = event.jaxis.which + 1;
	int axis 		= event.jaxis.axis;
	int value 		= event.jaxis.value;
	if(axis == JOYSTICK_X_AXIS) {
		if(value > JOYSTICK_DELTA )	{
			return INPUT_RIGHT;
		} else if (value  < - JOYSTICK_DELTA )	{
		} else {
			return INPUT_
		}
	}
	
	if(event.jaxis.axis == JOYSTICK_X_AXIS) 		{
		if(event.jaxis.value > JOYSTICK_DELTA )		

	} else if (event.jaxis.axis == JOYSTICK_Y_AXIS) {

	} else {
		
	}

	event.button  = event.jbutton.button;
	return event;
} 
*/

int Input::handle_events() {
	SDL_Event event;
	int index = 0;
	for(index = 0; index < this->handle_max; index ++) { 	
		int must_handle = SDL_PollEvent(&event);
		if(!must_handle) return 0;
	 	switch(event.type) { /* Process the appropiate event type */
    	case SDL_KEYDOWN:  /* Handle a KEYDOWN event */
      		printf("Key press: %d\n", event.key.keysym.sym );
      	break;
		case SDL_KEYUP:
			printf("Key release: %d\n", event.key.keysym.sym );
		break;
		case SDL_JOYBUTTONDOWN:
			printf("Joystick press: on %d : %d \n", event.jbutton.which, event.jbutton.button );
		break;
		case SDL_JOYBUTTONUP:
			printf("Joystick release: on %d : %d \n", event.jbutton.which, event.jbutton.button );
		break;
		case SDL_JOYAXISMOTION:
			printf("Joystick motion: on %d : %d to %d \n", 
					event.jaxis.which, event.jaxis.axis, event.jaxis.value );
		break;
    	case SDL_QUIT:
      		printf("Quitting...\n");
			exit(0);
		break;
   		default: /* Report an unhandled event */
      		printf("I don't know what this event is!\n");
		}
	}
	return index;
}

/* State based input processing . Joystick input overrides similar keyboard input. */
int Input::process() {
	SDL_PumpEvents();
	int numkeys;
	Uint8 *keystate = SDL_GetKeyState(&numkeys);
	for(Inputmap::iterator iter = map.begin(); iter != map.end(); iter++) {
		int realkey 	   = iter->first;
		int realkeystate   = keystate[realkey];
		Inputstate istate   = map[realkey];		
		this->state[istate.player][istate.button] =  realkeystate;
	} 
	for(int jsi = 0; jsi < joysticks.size() ; jsi++) {
		Joystickpointer js = joysticks[jsi];
		int player = jsi + 1;
		for (int btni = 0; btni < SDL_JoystickNumButtons(js); btni++) {
			state[player][btni] = SDL_JoystickGetButton(js, btni);
			// Read button states;
		}
		int x_value =  SDL_JoystickGetAxis(js, JOYSTICK_X_AXIS);
		int y_value =  SDL_JoystickGetAxis(js, JOYSTICK_Y_AXIS);
		if( x_value > JOYSTICK_DELTA) {
			state[player][INPUT_RIGHT] = 1;
			state[player][INPUT_LEFT]  = 0;
		} else if ( x_value < (- JOYSTICK_DELTA) ) {
			state[player][INPUT_RIGHT] = 0;
			state[player][INPUT_LEFT]  = 1;
		} else {
			state[player][INPUT_RIGHT] = 0;
			state[player][INPUT_LEFT]  = 0;
		}

		if( y_value > JOYSTICK_DELTA) {
			state[player][INPUT_DOWN] = 1;
			state[player][INPUT_UP]  = 0;
		} else if ( y_value < (- JOYSTICK_DELTA) ) {
			state[player][INPUT_DOWN] = 0;
			state[player][INPUT_UP]   = 1;
		} else {
			state[player][INPUT_DOWN] = 0;
			state[player][INPUT_UP]   = 0;
		}


		
	} 
	return 0;
}
